package webdev.fragment;

import webdev.common.Common;
import webdev.common.StoresValueCommon;
import webdev.database.SQLQuery;
import webdev.utils.ToastMessageUtil;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import dev.danang.R;

public class ChangePasswordFragment extends BaseFragment implements
		OnClickListener {

	EditText edtCurrentPass, edtNewPass, edtConfirmNewPass;
	Button btnSavePass;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View layout = inflater.inflate(R.layout.change_password_frament, null);
		init(layout);
		setValueForMembers();
		setEventForMembers();

//		// Load data
//		if (ConnectionDetectorUtil.isConnectingToInternet(getActivity())) {
//
//		} else {
//			// show message "No network connection"
//			ToastMessageUtil.showToastLong(getActivity(), getResources()
//					.getString(R.string.message_no_network));
//		}

		return layout;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @author: Truong.le
	 * 
	 * @to do: TODO
	 * 
	 * @see webdev.fragment.BaseFragment#init(android.view.View)
	 */
	@Override
	protected void init(View layout) {
		// TODO Auto-generated method stub
		edtCurrentPass = (EditText) layout.findViewById(R.id.edt_current_pass);
		edtNewPass = (EditText) layout.findViewById(R.id.edt_new_pass);
		edtConfirmNewPass = (EditText) layout
				.findViewById(R.id.edt_confirm_new_pass);
		btnSavePass = (Button) layout.findViewById(R.id.btn_save_pass);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @author: Truong.le
	 * 
	 * @to do: TODO
	 * 
	 * @see webdev.fragment.BaseFragment#setValueForMembers()
	 */
	@Override
	protected void setValueForMembers() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @author: Truong.le
	 * 
	 * @to do: TODO
	 * 
	 * @see webdev.fragment.BaseFragment#setEventForMembers()
	 */
	@Override
	protected void setEventForMembers() {
		// TODO Auto-generated method stub
		btnSavePass.setOnClickListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @author: Truong.le
	 * 
	 * @to do: TODO
	 * 
	 * @see webdev.fragment.BaseFragment#getUrl()
	 */
	@Override
	protected String getUrl() {
		// TODO Auto-generated method stub
		return null;
	}

	private void validateForm() {
		String passCurr = edtCurrentPass.getText().toString().trim();
		String passNewPass = edtNewPass.getText().toString().trim();
		String passConfirm = edtConfirmNewPass.getText().toString().trim();

		
		if (passNewPass == null || passNewPass.equals("")) {
			ToastMessageUtil.showMessage(
					getActivity(),
					getResources().getString(R.string.warning),
					getResources().getString(
							R.string.new_password_cannot_be_blank),
					getResources().getString(R.string.OK));
			return;
		}
		if (passConfirm == null || passConfirm.equals("")) {
			ToastMessageUtil.showMessage(
					getActivity(),
					getResources().getString(R.string.warning),
					getResources().getString(
							R.string.confirm_new_password_cannot_be_blank),
					getResources().getString(R.string.OK));
			return;
		}
		if (passCurr == null || passCurr.equals("")) {
			ToastMessageUtil.showMessage(
					getActivity(),
					getResources().getString(R.string.warning),
					getResources().getString(
							R.string.current_password_cannot_be_blank),
					getResources().getString(R.string.OK));
			return;
		}
		
		if (passNewPass.length()<6) {
			ToastMessageUtil.showMessage(
					getActivity(),
					getResources().getString(R.string.warning),
					getResources().getString(
							R.string.new_password_is_too_short),
					getResources().getString(R.string.OK));
			return;
		}
		if (passConfirm.length()<6) {
			ToastMessageUtil.showMessage(
					getActivity(),
					getResources().getString(R.string.warning),
					getResources().getString(
							R.string.confirm_new_password_is_too_short),
					getResources().getString(R.string.OK));
			return;
		}
		if (passCurr == null || passCurr.equals("")) {
			ToastMessageUtil.showMessage(
					getActivity(),
					getResources().getString(R.string.warning),
					getResources().getString(
							R.string.current_password_is_too_short),
					getResources().getString(R.string.OK));
			return;
		}
		if (passCurr == null || passCurr.equals("")) {
			ToastMessageUtil.showMessage(
					getActivity(),
					getResources().getString(R.string.warning),
					getResources().getString(
							R.string.current_password_is_too_short),
					getResources().getString(R.string.OK));
			return;
		}
		String passLogin = "";
		try {
			StoresValueCommon cls = new StoresValueCommon(getActivity());
			passLogin = cls.loadStringSavedPreferences("passwordUser");
			System.out.println("pass: "+ passCurr+"/"+passNewPass+"/"+passConfirm+"/"+passLogin);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!passNewPass.equals(passConfirm)) {
			
			ToastMessageUtil.showMessage(
					getActivity(),
					getResources().getString(R.string.warning),
					getResources().getString(
							R.string.confirm_new_password_must_be_repeated_exactly),
					getResources().getString(R.string.OK));
			return;
		}
		if (passLogin.equals(passCurr)) {
			 //new ProgressTaskChangePassword(state, user,passNewPass).execute();
			new AsyncChangePassword("","").execute();
		} else {
			ToastMessageUtil.showMessage(
					getActivity(),
					getResources().getString(R.string.warning),
					getResources().getString(
							R.string.the_current_password_is_incorrect),
					getResources().getString(R.string.OK));
			return;
		}
	}
	class AsyncChangePassword extends AsyncTask<Void, Void, Boolean> {
		ProgressDialog dialog;
		String userId, strNewPass;
		public AsyncChangePassword(String userId, String strNewPass) {
			this.userId = userId;
			this.strNewPass = strNewPass;
		}

		@Override
		public void onPreExecute() {
			dialog = new ProgressDialog(getActivity());
			dialog.setCancelable(false);
			dialog.setMessage(getResources().getString(R.string.processing));
			dialog.show();
		}

		@Override
		public Boolean doInBackground(Void... param) {
			return true;
		}

		@Override
		public void onPostExecute(Boolean res) {
			dialog.dismiss();
			if(res){
				ToastMessageUtil.showMessage(getActivity(), getResources().getString(R.string.message), getResources().getString(R.string.post_photo_success), getResources().getString(R.string.OK));
			}else{
				ToastMessageUtil.showMessage(getActivity(), getResources().getString(R.string.message), getResources().getString(R.string.post_photo_failed), getResources().getString(R.string.OK));
			}
			onCancelled();
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @author: Truong.le
	 * 
	 * @to do: TODO
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		if (v == btnSavePass) {
//			validateForm();
			changePassword();
		}

	}

	private void changePassword() {
		String currentPass = edtCurrentPass.getText().toString().trim();
		String newPass = edtNewPass.getText().toString().trim();
		String retypeNewPass = edtConfirmNewPass.getText().toString().trim();

		if ("".equals(currentPass)) {
			Toast.makeText(getActivity(), "Nhập mật khẩu hiện tại", Toast.LENGTH_SHORT).show();
			return;
		}
		if ("".equals(newPass)) {
			Toast.makeText(getActivity(), "Nhập mật khẩu mới", Toast.LENGTH_SHORT).show();
			return;
		}
		if ("".equals(retypeNewPass)) {
			Toast.makeText(getActivity(), "Nhập lại mật khẩu mới", Toast.LENGTH_SHORT).show();
			return;
		}
		if (newPass.equals(retypeNewPass) == false) {
			Toast.makeText(getActivity(), "Mật khẩu mới không trùng khớp", Toast.LENGTH_SHORT).show();
			return;
		}
		if (Common.currentUser.getmPassword().equals(currentPass) == false) {
			Toast.makeText(getActivity(), "Mật khẩu hiện tại không đúng", Toast.LENGTH_SHORT).show();
			return;
		}

		Common.currentUser.setmPassword(newPass);
		SQLQuery db = new SQLQuery(getActivity());
		db.updateUser(Common.currentUser);
		if (db.updateUser(Common.currentUser)) {
			Toast.makeText(getActivity(), "Thay đổi mật khẩu thành công", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(getActivity(), "Thay đổi mật khẩu không thành công", Toast.LENGTH_SHORT).show();
		}

		edtCurrentPass.setText("");
		edtNewPass.setText("");
		edtConfirmNewPass.setText("");
	}
}
